const cloud = require('wx-server-sdk');
cloud.init({
  env:"xiaotiao-6gcr9hxy3d8aa90f"
});
const db = cloud.database();
const _ = db.command;

// 创建集合云函数入口函数
exports.main = async (event, context) => {
  var openid = event.openid;
  var records=[];
  var bloodSugar = event.bloodSugar;//血糖
  // var bloodFat = event.bloodFat;//血脂
  var bloodPressure1 = event.bloodPressure1;//收缩压
  var bloodPressure2 = event.bloodPressure2;//舒张压
  var result = event.result;//复选框结果
  var other = event.other;//其他
  var color = event.color;//尿液颜色
  var uricProtein = event.uricProtein;//尿蛋白
  var creatinine = event.creatinine;//肌酐
  var uricVolume = event.uricVolume;//尿量
  var uricpH = event.uricpH;//尿ph
  var uricGravity = event.uricGravity;//尿比重
  //page4
  var alcohol = event.alcohol;
  var diet = event.diet;//饮食
  var emotion = event.emotion;//心情
  var sleep = event.sleep;//睡眠
  var sport = event.sport;//运动
  
  

  //查找当前数据库中是否存在这个openid
  return await db.collection("ClockInRecords").where({
    _openid:openid
  })
  .get().then(res=>{
    //如果找不到这个openid,就add进去
    if(res.data.length ==0){
      console.log("找不到openid")
      db.collection("ClockInRecords")
      .add({
        data:{
          _openid:openid,
          _records:[
            {
              _date:new Date()
            }
          ]
        }
      })
    }//如果找到了就update
      db.collection("ClockInRecords")
      .where({
        _openid:openid
      }).get().then(res=>{
        //更新user数据库
        records = res.data[0]._records
        let i = 0;
        for(i = 0;i<records.length;i++){
          if(records[i]._date.getFullYear() == new Date().getFullYear()
          && records[i]._date.getMonth() == new Date().getMonth()
          && records[i]._date.getDate() == new Date().getDate()){
            console.log("是同一天哇");
            records[i]._date = new Date()
            if(bloodSugar!=null&&bloodSugar.length>0)
            records[i]._bloodSugar = bloodSugar
            // if(bloodFat!=null&&bloodFat.length>0)
            // records[i]._bloodFat = bloodFat
            if(bloodPressure1!=null&&bloodPressure1.length>0)
            records[i]._bloodPressure1 = bloodPressure1
            if(bloodPressure2!=null&&bloodPressure2.length>0)
            records[i]._bloodPressure2 = bloodPressure2
            if(color!=null&&color.length>0)
            records[i]._color = color
            if(result!=null&&result.length>0)
            records[i]._result = result
            if(other!=null&&other.length>0)
            records[i]._other = other
            if(uricProtein!=null&&uricProtein.length>0)
            records[i]._uricProtein = uricProtein
            if(creatinine!=null&&creatinine.length>0)
            records[i]._creatinine = creatinine
            if(uricVolume!=null&&uricVolume.length>0)
            records[i]._uricVolume = uricVolume
            if(uricpH!=null&&uricpH.length>0)
            records[i]._uricpH = uricpH
            if(uricGravity!=null&&uricGravity.length>0)
            records[i]._uricGravity = uricGravity
            if(alcohol!=-1)
            records[i]._alcohol = alcohol
            if(diet!=-1)
            records[i]._diet = diet
            if(emotion!=-1)
            records[i]._emotion = emotion
            if(sleep!=-1)
            records[i]._sleep = sleep
            if(sport!=-1)
            records[i]._sport = sport
            break
          }
          //更新其他数据
        }
        //没有今日打卡记录
        if(i==records.length){
          console.log("今日没打卡")
          var temp={
            _date:new Date(),
            _bloodSugar:bloodSugar,
            // _bloodFat:bloodFat,
            _bloodPressure1:bloodPressure1,
            _bloodPressure2:bloodPressure2,
            _color:color,
            _result:result,
            _other:other,
            _uricProtein:uricProtein,
            _creatinine:creatinine,
            _uricVolume:uricVolume,
            _uricpH:uricpH,
            _uricGravity:uricGravity,
            _alcohol:alcohol,
            _diet:diet,
            _emotion:emotion,
            _sleep:sleep,
            _sport:sport
          }
          records.push(temp)
          //更新user数据表
          var dakadays=0
          db.collection("user").where({
            _openid:openid
          }).get().then(res=>{
            console.log(res.data[0].dakaDays)
            dakadays = res.data[0].dakaDays+1
            console.log(dakadays)
              db.collection("user").where({
                _openid:openid
              }).update({
                data:{
                  dakaDays:dakadays
                }
              }).then(res=>{
                console.log("更新user表成功")
              })
          })
          console.log("在外面")
        }
        //更新数据库
        db.collection("ClockInRecords")
      .where({
        _openid:openid
      }).update({
        data:{
          _records:records
        }
      }).then(res=>{
        console.log("更新数据成功！")
      }).catch(res=>{
        console.log(res=>data)
        console.log("失败！")
      })
      }).catch(res=>{
        console.log(res)
      })
    
  })
  
}
// isSameDay(){
//   return true;
// }